from main.ext import db
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
# from main import constants
class BaseModel(object):

    create_time = db.Column(db.DateTime,default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

class User(BaseModel,db.Model):
    """用户表"""
    __tablename__ = 'ih_user_profile'
    id = db.Column(db.Integer, primary_key=True, comment='用户编号')  #
    name = db.Column(db.String(32), unique=True, nullable=False, comment='用户名称')  #
    password_hash = db.Column(db.String(128), nullable=False, comment='加密的密码')  #
    mobile = db.Column(db.String(11), unique=True, nullable=False, comment='手机号')  #
    real_name = db.Column(db.String(32), comment='真实姓名')  #
    id_card = db.Column(db.String(20), comment='身份证号')  #
    avatar_url = db.Column(db.String(128), comment='用户头像路径')  #
    houses = db.relationship("House", backref="user", comment='用户发布的房屋')  #
    orders = db.relationship("Order", backref="user", comment='用户下的订单')  #

    @property
    def password(self):
        raise ArithmeticError("不可读")


class Area(BaseModel, db.Model):
    """城区"""

    __tablename__ = "ih_area_info"

    id = db.Column(db.Integer, primary_key=True, comment='区域编号')  #
    name = db.Column(db.String(32), nullable=False, comment='区域名字')  # 区域名字
    houses = db.relationship("House", backref="area", comment='区域的房屋')  # 区域的房屋


# 房屋设施表，建立房屋与设施的多对多关系
house_facility = db.Table(
    "ih_house_facility",
    db.Column("house_id", db.Integer, db.ForeignKey("ih_house_info.id"), primary_key=True),  # 房屋编号
    db.Column("facility_id", db.Integer, db.ForeignKey("ih_facility_info.id"), primary_key=True)  # 设施编号
)


class House(BaseModel, db.Model):
    """房屋信息"""

    __tablename__ = "ih_house_info"

    id = db.Column(db.Integer, primary_key=True, comment='房屋编号')  #
    user_id = db.Column(db.Integer, db.ForeignKey("ih_user_profile.id"), nullable=False, comment='房屋主人的用户编号')  #
    area_id = db.Column(db.Integer, db.ForeignKey("ih_area_info.id"), nullable=False, comment='归属地的区域编号')  #
    title = db.Column(db.String(64), nullable=False, comment='标题')  #
    price = db.Column(db.Integer, default=0, comment='单价，单位：分')  #
    address = db.Column(db.String(512), default="", comment='地址')  #
    room_count = db.Column(db.Integer, default=1, comment='房间数目')  #
    acreage = db.Column(db.Integer, default=0, comment='房屋面积')  #
    unit = db.Column(db.String(32), default="", comment='房屋单元')  # ， 如几室几厅
    capacity = db.Column(db.Integer, default=1, comment='房屋容纳的人数')  #
    beds = db.Column(db.String(64), default="", comment='房屋床铺的配置')  #
    deposit = db.Column(db.Integer, default=0, comment='房屋押金')  #
    min_days = db.Column(db.Integer, default=1, comment='最少入住天数')  #
    max_days = db.Column(db.Integer, default=0, comment='最多入住天数，0表示不限制')  #
    order_count = db.Column(db.Integer, default=0, comment='预订完成的该房屋的订单数')  #
    index_image_url = db.Column(db.String(256), default="", comment='房屋主图片的路径')  #
    facilities = db.relationship("Facility", secondary=house_facility, comment='房屋的设施')  #
    images = db.relationship("HouseImage", comment='房屋的图片')  #
    orders = db.relationship("Order", backref="house", comment='房屋的订单')  #


class Facility(BaseModel, db.Model):
    """设施信息"""

    __tablename__ = "ih_facility_info"

    id = db.Column(db.Integer, primary_key=True, comment='设施编号')  #
    name = db.Column(db.String(32), nullable=False, comment='设施名字')  #


class HouseImage(BaseModel, db.Model):
    """房屋图片"""

    __tablename__ = "ih_house_image"

    id = db.Column(db.Integer, primary_key=True)
    house_id = db.Column(db.Integer, db.ForeignKey("ih_house_info.id"), nullable=False, comment='房屋编号')  #
    url = db.Column(db.String(256), nullable=False, comment='图片的路径')  #


class Order(BaseModel, db.Model):
    """订单"""

    __tablename__ = "ih_order_info"

    id = db.Column(db.Integer, primary_key=True)  # 订单编号
    user_id = db.Column(db.Integer, db.ForeignKey("ih_user_profile.id"), nullable=False, comment='下订单的用户编号')  #
    house_id = db.Column(db.Integer, db.ForeignKey("ih_house_info.id"), nullable=False, comment='预订的房间编号')  #
    begin_date = db.Column(db.DateTime, nullable=False, comment='预订的起始时间')  #
    end_date = db.Column(db.DateTime, nullable=False, comment='预订的结束时间')  #
    days = db.Column(db.Integer, nullable=False, comment='预订的总天数')  #
    house_price = db.Column(db.Integer, nullable=False, comment='房屋的单价')  #
    amount = db.Column(db.Integer, nullable=False, comment='订单的总金额')  #
    status = db.Column(  # 订单的状态
        db.Enum(
            "WAIT_ACCEPT",  # 待接单,
            "WAIT_PAYMENT",  # 待支付
            "PAID",  # 已支付
            "WAIT_COMMENT",  # 待评价
            "COMPLETE",  # 已完成
            "CANCELED",  # 已取消
            "REJECTED"  # 已拒单
        ),
        default="WAIT_ACCEPT", index=True)
    comment = db.Column(db.Text, comment='订单的评论信息或者拒单原因')  #
